<?php
class Translate
{
  	private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
		$this->_db = Zend_Registry::get('db');
    }

  
	/**
	* Set object state
	* 
	* @param  array $options 
	* @return Default_Model_Translate
	*/
	public function setOptions(array $options)
		{
			//print_r($options);
			$methods = get_class_methods($this);
	  	foreach ($options as $key => $value) {
	      $method = 'set' . ucfirst($key);
	      if (in_array($method, $methods)) {
	          $this->$method($value);
	      }
	  }
	  return $this;
	}

	/**
	* Set contents
	* 
	* @param  string $text 
	* @return Default_Model_Translate
	*/
	public function setContents($text)
	{
	  $this->_contents = (string) $text;
	  return $this;
	}
	
	/**
	* Get contents
	* 
	* @return null|string
	*/
	public function getContents()
	{
	  return $this->_contents;
	}
  
  
	/**
	* Set language
	* 
	* @param  string $text 
	* @return Default_Model_Translate
	*/
	
	public function setLanguage($text)
	{
	  $this->_language = (string) $text;
	  return $this;
	}
	
	/**
	* Get post
	* 
	* @return null|string
	*/
	public function getLanguage()
	{
	  return $this->_language;
	}

	/**
	* Set entry timestamp
	* 
	* @param  string
	*/
	public function setDate_modified($ts)
	{
	  $this->_date_modified = $ts;
	  return $this;
	}
	
	/**
	* Get entry timestamp
	* 
	* @return string
	*/
	public function getDate_modified()
	{
	  return $this->_date_modified;
	}

	/**
	* Set entry id
	* 
	* @param  int $_id 
	* @return Default_Model_Translate
	*/
	public function setId($id)
	{
	  $this->_id = (int) $id;
	  return $this;
	}
	
	/**
	* Retrieve entry id
	* 
	* @return null|int
	*/
	public function getId()
	{
	  return $this->_id;
	}
  
	public function saveTranslate($form_values)
	{
		$contents = strip_tags($form_values['content']);
		$lang = $form_values['lang'];
		$id = $form_values['id'];
		//exit;
		if($lang == 'ru')
		{
			$field = 'ru_text';
		} elseif ($lang == 'en') {
			$field = 'en_text';
		}
		
		//$this->_db->query( "set names 'utf8'" ) ;
		
		if ($id != '') {
			$this->_db->query( "set names 'utf8'" ) ;
			$update_translation = $this->_db->exec("
			UPDATE td_labels 
			SET " . $field . " = '" . $contents . "'
			WHERE id = " . $id);
		} else {
			$this->_db->query( "set names 'utf8'" ) ;
			$add_translation = $this->_db->query("
			INSERT 
			INTO td_labels(id, " . $field . ")
			VALUES(?,?)", array($id, $contents));      
		}
		
		if($update_translation) {
			echo "Translation text has been successfully updated";
		} else if($add_translation) {
			echo "New Translation text has been sccussfully added";
		}
	}
  
  /**
   * Update the version history of the current translate
   *
   * @param array $form_values
   * @param int $id
   * @param int $version
   */
  
  
  public function updateVersion($form_values, $id, $version)
  {
    $contents = $form_values['contents'];  
    $id_author = Zend_Auth::getInstance()->getStorage()->read()->id_member; 
    $date_modified = date('Y-m-d H:i:s');
    $this->_db->query( "set names 'utf8'" ) ;
    $this->_db->query("
      UPDATE td_labels_version 
      SET id_author = " . $id_author . ", contents = '" . $contents . "', date_modified = now()
      WHERE id = ?
      AND version = ?", array($id, $version));    
  }
  
  
  /**
   * Add version history
   * 
   * @return void
   */
  public function addVersionHistory(Default_Model_Translate $Translate)
  {
      $data = array(
          'id'            => $Translate->getId(),
          'language'      => $Translate->getLanguage(),
          'base_version'  => $bv,
          'version'       => $version,
          'type'          => 'std',
          'id_author'     => Zend_Auth::getInstance()->getStorage()->read()->id_member, 
          'contents'      => $Translate->getContents(),
          'note'          => '',            
          'date_modified' => date('Y-m-d H:i:s'),
                            
      );
      
      if (null === ($id = $Translate->getId())) {
          unset($data['id']);
          $this->get_dbTable()->update($data, array('id = ?' => $id), array('language = ?' => $lang), array('id_author = ?' => $id_author), array('date_modified = ?' => now()));            
      } else {
      		$this->_db->query( "set names 'utf8'" ) ;
          $this->_db->query("            
          INSERT
          INTO td_labels_version     
          VALUES(?,?,?,?,?,?,?,?,?)", $data, array('id = ?' => $id), array('language = ?' => $lang), array('id_author = ?' => $id_author), array('date_modified = ?' => now()));
      }
  }

  /**
   * Find an entry
   *
   * Resets entry state if matching id found.
   * 
   * @param  int $id 
   * @return Default_Model_Translate
   */
  public function find($id)
  {
      $this->getMapper()->find($id, $this);
      return $this;
  }

  /**
   * Fetch translated text
   * 
   * @return array
   */
  
  public function fetchTranslateText()
  {
  	$this->_db->query( "set names 'utf8'" ) ;
  	$qry = "SELECT tl.id, 
  			   tlk.keyText, 
  			   tl.en_text, 
  			   tl.ru_text  
  			FROM td_labels tl 
  			INNER JOIN td_labels_key tlk ON ( tl.id = tlk.id)";
  	
  	$resultSet = $this->_db->fetchAll($qry);
  	return $resultSet;		
	}
	
	/**
   * Fetch translate key for edit
   * 
   * @param int $id
   * @param string $lang
   * @return array
  */ 
	
	public function fetchTranslateEditText($id, $lang)
	{
	  	$this->_db->query( "set names 'utf8'" ) ;
		$resultSet = $this->_db->fetchAll("
		  SELECT lbl.en_text, lbl.ru_text, lk.keyText
		  FROM td_labels AS lbl
		  LEFT JOIN td_labels_key AS lk ON lk.id = lbl.id
		  WHERE lbl.id = ".$id);  				
		return $resultSet;
	}
	
	/**
   * Fetch translate version key
   * 
   * @param array $where_clause
   * @return array
  */ 
	
	public function fetchTranslateVersion($where_clause) {
	  	$this->_db->query( "set names 'utf8'" ) ;
		$resultSet = $this->_db->fetchAll("
		  SELECT lv.id as version_id,
		         lv.language as version_lang, 
		         lv.version, 
		         lv.id_author, 
		         lv.contents,
		         lv.date_modified	             
      FROM td_labels_version AS lv      
      " . $where_clause);
    return $resultSet;
	}
	
	/**
   * Fetch translate version key for edit
   * 
   * @param int $id
   * @param string $version
   * @return array
  */ 
	
	public function fetchVersionEditText($id, $version)
	{
	  	$this->_db->query( "set names 'utf8'" ) ;
		$resultSet = $this->_db->fetchAll("
		  SELECT lv.base_version,
		         lv.version, 
		         lv.id_author, 
		         lv.contents,
		         lv.date_modified
		  FROM td_labels_version AS lv
		  WHERE lv.id = " . $id . " 
		    AND lv.version = '" . $version . "' ");  				
		return $resultSet;
	}
	
	/**
	 * Fetch all supported languages for the translate admin panel
	 * @return array
	 */
	public function fetchSupportedLang()
	{
		$this->_db->query( "set names 'utf8'" ) ;
		$resultSet = $this->__db->fetchAll("
			SELECT ln.language_id
			FROM languages ln
			WHERE ln.is_published = ?
			GROUP BY ln.language_id", array(1));
			
		return $resultSet;
	}
	
	public function getTranslateTextBySection($section)
	{
		$this->_db->query("SET NAMES 'utf8'");
		$translateTextArr = $this->_db->fetchAll("
			SELECT lbl.id, lbl.en_text, lbl.ru_text, lk.keyText
			  FROM td_labels AS lbl
			  LEFT JOIN td_labels_key AS lk ON lk.id = lbl.id
			  WHERE lbl.section = '".$section."'  
		");
		
		return $translateTextArr;
	}
}
